package be.bejug.scrumadmin.fieldfactory;

import com.vaadin.data.Validator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.DateField;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

/**
 * Builder class for FieldFactories.
 * 
 * @author GDAFJ73
 * 
 */
public class FieldBuilder {
	/**
	 * Creates a combobox.
	 * 
	 * @param caption
	 *            the caption
	 * @param items
	 *            the items of the combobox
	 * @return the generated combo box
	 */
	public static ComboBox buildComboBox(final String caption,
			final boolean readOnly, final Object... items) {
		ComboBox comboBox = new ComboBox();
		comboBox.setReadOnly(readOnly);
		comboBox.setCaption(caption);

		if ((items != null) && (items.length > 0)) {
			for (Object item : items) {
				comboBox.addItem(item);
			}
		}

		return comboBox;
	}

	/**
	 * Creates a required combobox.
	 * 
	 * @param caption
	 *            the caption
	 * @param errorMessage
	 *            the required error message
	 * @param items
	 *            the items of the combobox
	 * @return the generated required combo box
	 */
	public static ComboBox buildRequiredComboBox(final String caption,
			final boolean readOnly, final String errorMessage,
			final Object... items) {
		ComboBox comboBox = buildComboBox(caption, readOnly, items);
		comboBox.setRequired(true);
		comboBox.setRequiredError(errorMessage);

		return comboBox;
	}

	/**
	 * Creates a textfield.
	 * 
	 * @param caption
	 *            the caption
	 * @param readOnly
	 *            readOnly or not
	 * @param validators
	 *            some validators
	 * @return the generated text field
	 */
	public static TextField buildTextField(final String caption,
			final boolean readOnly, final Validator... validators) {
		TextField textField = new TextField();
		textField.setCaption(caption);
		textField.setNullRepresentation("");
		textField.setColumns(20);
		textField.setReadOnly(readOnly);
		textField.setCaption(caption);

		if ((validators != null) && (validators.length > 0)) {
			for (Validator validator : validators) {
				textField.addValidator(validator);
			}
		}

		return textField;
	}

	/**
	 * Creates a required textfield.
	 * 
	 * @param caption
	 *            the caption
	 * @param errorMessage
	 *            the required error message
	 * @param validators
	 *            some validators
	 * @return the generated required text field
	 */
	public static TextField buildRequiredTextField(final String caption,
			final boolean readOnly, final String errorMessage,
			final Validator... validators) {
		TextField textField = buildTextField(caption, readOnly, validators);
		textField.setRequired(true);
		textField.setRequiredError(errorMessage);

		return textField;
	}

	/**
	 * Creates a multiline textfield.
	 * 
	 * @param caption
	 *            the caption
	 * @param readOnly
	 *            readOnly or not
	 * @param validators
	 *            some validators
	 * @return the generated multiline text field
	 */
	public static TextArea buildTextArea(final String caption,
			final boolean readOnly, final Validator... validators) {
		TextArea textArea = new TextArea();
		textArea.setNullRepresentation("");
		textArea.setRows(5);
		textArea.setColumns(20);
		textArea.setReadOnly(readOnly);
		textArea.setCaption(caption);

		if ((validators != null) && (validators.length > 0)) {
			for (Validator validator : validators) {
				textArea.addValidator(validator);
			}
		}

		return textArea;
	}

	/**
	 * Creates a required textarea.
	 * 
	 * @param caption
	 *            the caption
	 * @param errorMessage
	 *            the required error message
	 * @param validators
	 *            some validators
	 * @return the generated required textarea
	 */
	public static TextArea buildRequiredTextArea(final String caption,
			final boolean readOnly, final String errorMessage,
			final Validator... validators) {
		TextArea textArea = buildTextArea(caption, readOnly, validators);
		textArea.setRequired(true);
		textArea.setRequiredError(errorMessage);

		return textArea;
	}

	/**
	 * Creates a date field.
	 * 
	 * @param caption
	 *            the caption
	 * @param readOnly
	 *            readOnly or not
	 * @param validators
	 *            some validators
	 * @return the generated date field
	 */
	public static DateField buildDateField(final String caption,
			final boolean readOnly, final Validator... validators) {
		DateField dateField = new DateField();
		dateField.setDateFormat("dd-MM-yyyy");
		dateField.setReadOnly(readOnly);
		dateField.setCaption(caption);

		return dateField;
	}

	/**
	 * Creates a required date field.
	 * 
	 * @param caption
	 *            the caption
	 * @param errorMessage
	 *            the error message
	 * @param validators
	 *            some validators
	 * @return the generated required date field
	 */
	public static DateField buildRequiredDateField(final String caption,
			final boolean readOnly, final String errorMessage,
			final Validator... validators) {
		DateField dateField = buildDateField(caption, readOnly, validators);
		dateField.setRequired(true);
		dateField.setRequiredError(errorMessage);

		return dateField;
	}
}
